package com.liming.cartype.core.biz;

import com.liming.cartype.api.biz.IParameterService;
import com.liming.cartype.api.persistence.impl.ParameterBeanImpl;
import com.liming.core.util.config.CouchdbConfig;
import org.lightcouch.Attachment;
import org.lightcouch.CouchDbClient;
import org.lightcouch.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @类描述：品牌服务实现类
 * @创建人：wangkr
 * @创建时间：2014-04-04 下午2:42
 * @修改人：
 * @修改时间：
 * @修改备注：
 */

public class ParameterServiceImpl implements IParameterService {

    private static CouchDbClient dbClient;


    public void init() {
        CouchdbConfig.getCouchdbConfig().setDbName("carinfo");
        dbClient = new CouchDbClient(
                CouchdbConfig.getCouchdbConfig().getDbName(),
                true,
                "http",
                CouchdbConfig.getCouchdbConfig().getHost(),
                CouchdbConfig.getCouchdbConfig().getPort(),
                CouchdbConfig.getCouchdbConfig().getUsername(),
                CouchdbConfig.getCouchdbConfig().getPassword());
    }


    @Override
    public ParameterBeanImpl addParameter(ParameterBeanImpl message) {
        Response response = dbClient.save(message);
        ParameterBeanImpl foo = dbClient.find(ParameterBeanImpl.class, response.getId());
        return foo;
    }

    @Override
    public ParameterBeanImpl updateParameter(ParameterBeanImpl message) {
        Response response = dbClient.update(message);
        ParameterBeanImpl foo = dbClient.find(ParameterBeanImpl.class, response.getId());
        return foo;
    }

    @Override
    public ParameterBeanImpl putParameter(ParameterBeanImpl message) {
        if (null != message.getRevision()) {
            return updateParameter(message);
        } else {
            return addParameter(message);
        }
    }

    @Override
    public ParameterBeanImpl findParameter(String id) {
        if(null != id && !"".equals(id)){
            ParameterBeanImpl foo = dbClient.find(ParameterBeanImpl.class, id);
            return foo;
        }else{
            return null;
        }

    }

    @Override
    public void deleteParameter(String id) {
        ParameterBeanImpl foo = dbClient.find(ParameterBeanImpl.class, id);
        dbClient.remove(foo);
    }

    @Override
    public List<ParameterBeanImpl> getAllParameter() {
        List<ParameterBeanImpl> list ;
        try {
            list = dbClient.view("parameter/findAllParameter").includeDocs(true).query(ParameterBeanImpl.class);
        } catch (Exception e) {
            return new ArrayList<ParameterBeanImpl>();
        }
        if (null != list && list.size() <= 0) {
            return new ArrayList<ParameterBeanImpl>();
        }
        return list;
    }

    @Override
    public String getAttachmentUrl(ParameterBeanImpl message, String fileName) {
        String attachMentUrl = "http://127.0.0.1:5984/carinfo";
        ParameterBeanImpl ParameterBean = message;
        Map<String, Attachment> attachmentMap1 = ParameterBean.getAttachments();
        for (String key : attachmentMap1.keySet()) {
            if (fileName.equals(key)) {
                attachMentUrl = attachMentUrl + "/" + ParameterBean.getId() + "/" + fileName;
                break;
            }
        }
        return attachMentUrl;
    }

    @Override
    public void destroy() {
        dbClient.shutdown();
    }
}


